package com.unhz.taigu.controller.permission;

import com.unhz.generator.pojo.SysPermission;
import com.unhz.generator.pojo.SysRole;
import com.unhz.generator.utils.EmptyUtil;
import com.unhz.taigu.service.SysPermissionService;
import com.unhz.taigu.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created by cxg on 2017/11/14 0014.
 *
 * @Author 曹显贵
 * @Date 2017/11/14 0014 16:11
 */
@Controller
@RequestMapping("/rolePermissionController")
public class SysRolePermissionController {
    static final int CHANGE_ID_1 = 1;
    static final int CHANGE_ID_2 = 2;
    //修改角色及名称的修改
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysPermissionService sysPermissionService;
    @RequestMapping("/findPermission")
    public String findPermission(Model model,@RequestParam("roleId") Integer roleId){

        SysRole sysRole = sysRoleService.selectByRoleId(roleId);
        //查询所有的权限
        List<SysPermission> sysPermissionList = sysPermissionService.selectPermission();
        //查询所有的菜单
        List<SysPermission> menusList = sysPermissionService.selectMenus();
        //应该查询角色是否有权限啊
        List<SysPermission> userpermission = sysPermissionService.selectPermissionByRoleId(roleId);
        //查询用户菜单（如果角色用户没有一个权限,进行品判断啊！）
        List<SysPermission> usermenus = sysPermissionService.selectMenusByRoleId(roleId);
        //todo 区分用户拥有的权限
        for(SysPermission sysPermission : sysPermissionList){
            sysPermission.setChangeId(CHANGE_ID_1);
        }
        if(!EmptyUtil.isEmpty(userpermission)){
            for(SysPermission sysPermission : userpermission){
                sysPermission.setChangeId(CHANGE_ID_2);
            }
            for(SysPermission sysPermission : sysPermissionList){
                for(SysPermission sysPermission1 : userpermission){
                    if(sysPermission.getId() == sysPermission1.getId()){
                        sysPermission.setChangeId(CHANGE_ID_2);
                    }
                }
            }
        }
        //todo 区分用户拥有的菜单
        for(SysPermission sysPermission : menusList){
            sysPermission.setChangeId(CHANGE_ID_1);
        }
        if(!EmptyUtil.isEmpty(usermenus)){
            for(SysPermission sysPermission : usermenus){
                sysPermission.setChangeId(CHANGE_ID_2);
            }
            for(SysPermission sysPermission : menusList){
                for(SysPermission sysPermission1 : usermenus){
                    if(sysPermission.getId() == sysPermission1.getId()){
                        sysPermission.setChangeId(CHANGE_ID_2);
                    }
                }
            }
        }
        model.addAttribute("permissionList", sysPermissionList);
        model.addAttribute("menuList",menusList);
        model.addAttribute("role", sysRole);

        return "system/quanxian/quanxianPermission-update.jsp";
    }

}
